;if (!Array.prototype.includes) {
    Array.prototype.includes = function() {
        'use strict';
        return Array.prototype.indexOf.apply(this, arguments) !== -1;
    };
};var timestamp = function (){
    var date = new Date();
    var timestamp = date.getTime();
    return timestamp;
};
var R_ID = [];
var readed_files = [];
var reader = new FileReader();
reader.onload = function (event) {
    on_drop(event.target.result);
};
var read_file = function(){
    let f = drag_files[0];
    reader.readAsText(f);
    return readed_files.pop();
};
var drag_files= [];

var rpc = {
    invoke : function(arg) { 
        let a = JSON.stringify(arg);
        // console.log(a);
        window.external.invoke(a); 
    },
    log: function(msgs) {
        let msg =msgs;
        rpc.invoke({
            tp: "[[CONSOLE-MSG-FROM-APP]]",
            id: "[[CONSOLE-MSG-FROM-APP]]",
            content:msg
        })
    },
    progress: function(arg){
        if ($("div#progressbar").attr("last") == null){
            $("div#progressbar").animate({
                left:"2%"
            },700);
        }
        setTimeout(function(){
            let last = $("div#progressbar").attr("last");
            let now = timestamp();
            if (now -last >= 3990){
                $("div#progressbar").animate({
                    left:"-3%"
                },1000);
                $("div#progressbar").attr("last",null);
            }
        },4000);
        $("div#progressbar").attr("last", timestamp());        
        $("div#progressbar > div#progressbar-now").animate({
            height: arg.progress + "%"
        },500 ,function(){
            $("div#progressbar").attr("height", arg.progress % 101 + "%");
        });
    },
    render : function(items) {
        console.log(items);
        let b = Base64.decode(items);
        console.log(b);
        let obj = JSON.parse(b);
        if (obj.progress != null){
            rpc.progress(obj)   
        }
        // obj.content = (obj.content);
        handle_json(obj);
        rpc.init();
    },
    render_list: function(items_str) {
        $(".list-group").remove();
        document.body.innerHTML += items_str;
        rpc.init();
    },
    init: function(){
        Ins = document.getElementsByTagName("input");
        for(i=0; i< Ins.length ; i++ ){
            if ($(Ins[i]).attr("listend") == null && R_ID.includes(Ins[i].id) ){
                    $(Ins[i]).keyup(function(evt){
                    // Ins[i].addEventListener("input", function (evt) {
                        console.log(evt);
                        rpc.invoke({
                            tp: 'text',
                            content: this.value,
                            id: this.id
                        });
                    });
                    // Ins[i].listend = "input";
                    $(Ins[i]).attr("listend", "input");
                
            }
            
        }
        Btns = document.getElementsByClassName("btn");
        for(i=0; i< Btns.length ; i++ ){
            if ($(Btns[i]).attr("listend") == null && R_ID.includes(Btns[i].id)){
                    $(Btns[i]).click(function(){
                    // Btns[i].addEventListener("click", function () {
                        rpc.invoke({
                            tp: 'btn',
                            content: this.value,
                            id: this.id
                            
                        });
                    })
                
                
                $(Btns[i]).attr("listend","btn");
            }
        }

        Listitems = document.getElementsByClassName("list-group-item");
        for(i=0; i< Listitems.length ; i++ ){
            if ($(Listitems[i]).attr("listend") == null && R_ID.includes(Listitems[i].id)){
                    $(Listitems[i]).click(function(){
                    // Listitems[i].addEventListener("click", function () {
                        click(this.id);
                    })
                    $(Listitems[i]).attr("listend","lclick");
                
            }
        }

        $(".list-group-item").dblclick(function(){
            dbclicklistener(this);
            click(this.id, 'db');
            do_some("db", this.id)
        });

        // document.addEventListener('DOMContentLoaded', (event) => {
        //     document.querySelectorAll('pre code').forEach((block) => {
                
        //     });
        // });
        var box = document.getElementById('drag-box');
        if (box != null && OsName != "Windows"){
            box.addEventListener("dragenter", function(e){
                e.preventDefault();
                if(on_drag != null){
                    on_drag(e)
                }
                
            }, false);
            
            box.addEventListener("drop",function(e){ 
                form = new FormData();
                e.preventDefault(); 
                var fileList = e.dataTransfer.files; 
                console.log(e.dataTransfer)
                console.log(fileList);
                if(fileList.length == 0){ 
                    return false; 
                }
                // if(on_drop != null){
                //     on_drop(e);
                // }

                // drag_files.push();
                reader.readAsText(fileList[0])
                // upload_files.push(fileList[0])
                // status++
                // console.log(status)
                // if (status == 1){
                //     $("#txt").text("把跑的数据文件拖到这里，每一行会分配给一个工具人")
                // }else if (status == 2){
                //     $("#box-content").html('<div class="form-group"><label for="">任务描述，必填</label><textarea class="form-control" id="session-desc" rows="3"></textarea><button type="submit" class="btn btn-primary" onclick="return DoUpdate()" >建立任务</button></div>')
                // }else{
                //     $("#box-content").html('<h4 id="txt">把脚本拖到这里</h4>')
                //     status = 0
                // }
            },false); 
            
        }

    }
}

var h_style = function(id){
    return hljs.highlightBlock($(id)[0])
}

var do_some = function(tp,id, content){
    console.log("empty do someing")
}

var handle_json = function(obj){
    console.log(obj);
}

var list_add = function(string){
    let list = document.getElementsByClassName("list-group-flush")[0];
    if (list != null){
        list.innerHTML =  '<li class="list-group-item" ><p>' + string+ '</p></li>' + list.innerHTML
    }
    
}

var list_add_all = function(ls){
    let list = document.getElementsByClassName("list-group-flush")[0];
    if (list != null){
        for(i=0; i < ls.length; i++ ){
            list.innerHTML = '<li class="list-group-item" ><p>' + ls[i] + '</p></li>' + list.innerHTML
        }
        
    }
}



var dbclicklistener = function(e){
    $(e).animate({
        // "margin-left":"360px",
        "margin-top":"-60px",
        "opacity": 0.3
      }, 500, function() {
        $(e).remove()
    });
}


function click(id, tp) {
    console.log("click id", id);
    let ele = document.getElementById(id);
    if (ele == null){
        return;
    }
    if (ele.id == null){
        ele.id = "no_id";
    }
    if (tp == null){
        rpc.invoke({
            tp:"btn",
            content:'click',
            id:ele.id
            }
        )
    }else{
        rpc.invoke({
            tp:"btn",
            content:tp,
            id:ele.id
            }
        )
    }   
}



var dz = $('#drag-box');
$(document).on({ 
    dragleave:function(e){    //拖离 
        e.preventDefault(); 
    }, 
    drop:function(e){  //拖后放 
        e.preventDefault(); 
    }, 
    dragenter:function(e){    //拖进 
        e.preventDefault(); 
    }, 
    dragover:function(e){    //拖来拖去 
        e.preventDefault(); 
    } 
}); 

console.oldLog = console.log;
console.log = function(msgs) {
    console.oldLog(msgs);
    let msg = msgs;
    rpc.log(msg);
};
window.onload = function() { rpc.init(); };
console.log("start app");